<template>
  <el-form-item prop="domains" :label="hasLabel ? '域名' : void 0" class="mb-6">
    <div class="flex flex-col w-[400px]">
      <el-input
        v-model="value"
        type="textarea"
        class="domain-input"
        resize="none"
        :placeholder="placeholder"
      />
      <div class="w-full flex justify-between mt-2">
        <div class="inline-block text-xs text-[#868D9D]">
          {{ domainTotal }} 条
        </div>
        <div
          class="ml-4 flex items-center cursor-pointer select-none"
          :class="
            domainTotal ? 'text-[#454954] hover:opacity-80' : 'text-[#C5CBD8]'
          "
          @click="value = ''"
        >
          <app-icon name="clear" :color="domainTotal ? '#595E70' : '#C5CBD8'" />
          <div class="text-xs ml-[6px]">清空记录</div>
        </div>
      </div>
    </div>
  </el-form-item>
</template>

<script setup lang="ts">
import { useDomainsTotal } from '../hooks/useDomainsTotal'
import { DOMAIN_INPUT_PLACEHOLDER } from '../utils/constants'

interface IProps {
  modelValue: string
  hasLabel?: boolean
}

const props = defineProps<IProps>()
interface IEmit {
  (event: 'update:modelValue', value: string): void
}

const emit = defineEmits<IEmit>()

const placeholder = inject(
  DOMAIN_INPUT_PLACEHOLDER,
  '输入需要添加的域名，每行一个，最多添加1000个'
)

const value = computed({
  get: () => props.modelValue,
  set(val) {
    emit('update:modelValue', val)
  }
})

const domainTotal = useDomainsTotal(() => value.value)
</script>

<style lang="scss" scoped>
.domain-input {
  :deep(.el-textarea__inner) {
    height: 140px;
  }
}
</style>
